package cn.cangfei.massage.controller;


import cn.cangfei.massage.common.api.CommonResult;
import cn.cangfei.massage.dto.param.MassageProductCategoryParam;
import cn.cangfei.massage.entity.MassageProductCategory;
import cn.cangfei.massage.service.IMassageProductCategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "分类模块", tags = "推拿馆产品分类接口")
@RestController
@CrossOrigin
@RequestMapping("/massage")
public class MassageProductCategoryController {

    @Resource
    private IMassageProductCategoryService massageProductCategoryService;


    // 添加分类
    @ApiOperation(value = "添加分类", notes = "传入 分类的对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "分类信息参数（名称，描述）", required = true, dataType = "MassageProductCategoryParam")
    })
    @RequestMapping(value = "/productCategory", method = RequestMethod.POST)
    public CommonResult insertProductCategory(@RequestBody MassageProductCategoryParam param) {

        String name = param.getName();


        if (ObjectUtils.isEmpty(name)) {
            return CommonResult.failed("请填写名称");
        }

        if (massageProductCategoryService.countByName(name)) {
            return CommonResult.validateFailed("产品名称已存在！");
        }

        if (massageProductCategoryService.insertProductCategory(param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");

    }

    // 修改分类
    @ApiOperation(value = "修改分类", notes = "传入 分类的对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类编号", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "param", value = "分类信息参数（名称，描述）", required = true, dataType = "MassageProductCategoryParam")
    })
    @RequestMapping(value = "/productCategory/{id}", method = RequestMethod.PUT)
    public CommonResult updateProductCategory(@PathVariable("id") Long id, @RequestBody MassageProductCategoryParam param) {

        String name = param.getName();

        // 根据id查询分类是否存在
        if (!massageProductCategoryService.countById(id)) {
            return CommonResult.validateFailed("分类不存在！");
        }

        if (ObjectUtils.isEmpty(name)) {
            return CommonResult.failed("请填写名称");
        }

        if (massageProductCategoryService.updateProductCategory(id, param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");

    }

    // 查询全部分类
    @ApiOperation(value = "查询全部分类", notes = "传入 分类的对象")
    @RequestMapping(value = "/productCategory", method = RequestMethod.GET)
    public CommonResult selectAllProductCategory() {
        List<MassageProductCategory> massageProductCategories = massageProductCategoryService.selectAllProductCategory();
        return CommonResult.success(massageProductCategories);

    }

    // 分页条件查询分类
    @ApiOperation(value = "分页条件查询分类", notes = "传入 分类的对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "分类名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/productCategoryWithPage", method = RequestMethod.GET)
    public CommonResult selectProductCategoryByPage(@RequestParam(value = "name", required = false) String name,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<MassageProductCategory> massageProductCategories = massageProductCategoryService.selectProductCategoryByPage(name, pageNum, pageSize);
        return CommonResult.success(massageProductCategories);

    }

    // 删除分类
    @ApiOperation(value = "删除分类", notes = "传入 分类的对象")
    @ApiImplicitParam(name = "id", value = "分类编号", required = true, dataType = "Long")
    @RequestMapping(value = "/productCategory/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteProductCategory(@PathVariable("id") Long id) {
        if (massageProductCategoryService.deleteProductCategory(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    // 批量删除分类
    @ApiOperation(value = "批量删除分类", notes = "传入 分类的对象")
    @ApiImplicitParam(name = "ids", value = "分类编号", required = true, dataType = "List<Long>")
    @RequestMapping(value = "/productCategories", method = RequestMethod.DELETE)
    public CommonResult deleteProductCategories(@RequestParam("ids") List<Long> ids) {
        if (massageProductCategoryService.deleteProductCategories(ids)) {
            return CommonResult.success();
        }
        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    // 根据id查询分类
    @ApiOperation(value = "根据id查询分类", notes = "传入 分类的对象")
    @ApiImplicitParam(name = "id", value = "分类编号", required = true, dataType = "Long")
    @RequestMapping(value = "/getProductCategory/{id}", method = RequestMethod.GET)
    public CommonResult selectProductCategoryById(@PathVariable("id") Long id) {
        MassageProductCategory massageProductCategory = massageProductCategoryService.selectProductCategoryById(id);
        if (ObjectUtils.isEmpty(massageProductCategory)) {
            return CommonResult.failed("分类不存在！");
        }
        return CommonResult.success(massageProductCategory);
    }
}
